package kgame5.k2d {
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	/**
	 * @author max
	 * 2010-12-14
	 * 跟交互相关的，能检测内部事件
	 * 摄像机拖拽器
	 * 因为是25D,所以直接拖拽
	 * 
	 * TODO 把world内的各个物体做成mouseEanbel=false,mouseChildre=false能提高性能（如有必要）
	 * TODO 不能超过边界
	 */
	public class MouseInput2D{
	
		private var mc:DisplayObject;//鼠标在其上拖动，则视窗变化
		
		private var camera:Camera2D;
		
		private var preX:Number;//注意如果是int,则拖动时会出现偏差！！！
		private var preY:Number;
		
		public function MouseInput2D(mc_world:DisplayObject,camera:Camera2D){
			
			this.mc=mc_world;
			this.camera =camera;
			
			mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			mc.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			mc.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private function onMouseDown(e:MouseEvent):void{
			preX=e.localX;
			preY=e.localY;
			
			
			mc.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		private function onMouseMove(e:MouseEvent):void{
			
			camera.target.x+=-(e.localX-preX);
			camera.target.y+=-(e.localY-preY);
			
			preX=e.localX;
			preY=e.localY;
		}
		
		private function onMouseUp(e:MouseEvent):void{
			mc.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}

		
		//TODO 还没实现呢
		private function onMouseClick(e:MouseEvent):void{
//			T.p("onMouseClick");
//			var x:Number=e.stageX+ camera.viewRect.x;
//			var y:Number=e.stageY+ camera.viewRect.y;
//			
//			T.p("stage",e.stageX,e.stageY,"viewRect",camera.viewRect);
//			T.p("screen",screenX,screenY);
//			
//			var v3d_p:Vector3D=new Vector3D();
//			Util25D.screenToWorld(screenX,screenY, v3d_p);
//			
//			T.p("world",v3d_p);
//			
//			var result:Array=[];
//			Util25D.queryPoint(v3d_p, world, result);
//			for each(var node:Node25D in result){
//				node.dispatchEvent(new KEvent(Node25D.CLICK,v3d_p));
//			}
//			//this.dispatchEvent(new KEvent(CLICK_ITEM,result));
		}
	}//end of class
}
